# -*- coding:utf-8 -*-
#! python3

# FaceCat-Python
#Shanghai JuanJuanMao Information Technology Co., Ltd 

from facecat_objc import *

#绘制视图
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaint(view, paint, clipRect):
	if view.viewName == "Button4":
		paint.fillRect("rgb(255,0,0)", 0, 0, view.size.cx, view.size.cy)
	else:
		onPaintDefault(view, paint, clipRect)

#视图的鼠标点击方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onClick(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks):
	onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)
	if view.viewName == "Button":
		print(view.text)
	elif view.viewName == "Button2":
		print(view.text)
	elif view.viewName == "Button3":
		print(view.text)
	elif view.viewName == "Button4":
		print(view.text)

gPaint = FCPaint() #创建绘图对象
gPaint.onPaint = onPaint 
gPaint.onClick = onClick

#加载示例
def loadUI():
	xml = """<?xml version="1.0" encoding="utf-8"?>
	<html xmlns="facecat">
	<head>
	</head>
	<body>
		<div dock="fill" type="tab" selectedindex="0">
		<div name="TabPage" text="可视化生成" type="tabpage">
			<input type="button" name="Button" text="取消" location="35,50" size="100,20" backcolor="rgb(170,178,189)" cornerradius="4" textcolor="rgb(255,255,255)" />
			<input type="button" name="Button2" text="确认" location="32,92" size="130,38" font="Default,16.0" cornerradius="10" hoveredcolor="rgb(100,100,100)" backcolor="rgb(59,174,218)" bordercolor="rgb(255,0,0)" textcolor="rgb(255,255,255)" cursor="Hand" />
			<input type="button" name="Button3" text="查询" location="33,155" size="100,42" textcolor="rgb(0,0,5)" backcolor="rgb(140,192,81)" font="Default,14.0" cornerradius="10" cursor="WaitCursor" />
		</div>
		<div name="TabPage2" text="代码创建" type="tabpage" />
		<div name="TabPage3" text="自定义重绘" type="tabpage">
			<input type="custom" cid="mybutton" name="Button4" text="删除" location="74,84" size="100,40" />
		</div>
		</div>
	</body>
	</html>
	"""
	renderFaceCat(gPaint, xml)
	#获取第二页
	tabPage = findViewByName("TabPage2", gPaint.views)
	button = FCButton()
	button.location = FCPoint(100, 100)
	button.size = FCSize(100, 20)
	addViewToParent(button, tabPage)
	button.text = "确定"

class AppDelegate(NSObject):
	def windowWillClose_(self, notification):
		os._exit(0)
	def applicationDidFinishLaunching_(self, notification):
		gPaint.size = FCSize(900, 770)
		window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
		NSMakeRect(0, 0, gPaint.size.cx, gPaint.size.cy),
		15,
		NSBackingStoreBuffered,
		False
		)
		window.setDelegate_(self)
		window.orderFrontRegardless()
		view = FCUIView.alloc().initWithFrame_(NSMakeRect(0, 0, gPaint.size.cx, gPaint.size.cy))
		view.paint = gPaint
		window.setContentView_(view)
		gPaint.view = view
		loadUI()

app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
app.run()
